unit untFrmPermissoesGrupos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, DBCtrls, StdCtrls, Buttons, DB;

type
  TFrmPermissoesGrupos = class(TForm)
    Panel1: TPanel;
    btnFechar: TBitBtn;
    dsLookupGrupos: TDataSource;
    tvMenus: TTreeView;
    Panel3: TPanel;
    Label4: TLabel;
    Label1: TLabel;
    dbGrupos: TDBLookupComboBox;
    Panel2: TPanel;
    GroupBox1: TGroupBox;
    cbAlterar: TCheckBox;
    cbConsultar: TCheckBox;
    cbIncluir: TCheckBox;
    cbExcluir: TCheckBox;
    CbTodos: TCheckBox;
    btnSalvar: TBitBtn;
    btnDesfazer: TBitBtn;
    procedure btnFecharClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure dbGruposKeyPress(Sender: TObject; var Key: Char);
    procedure dbGruposCloseUp(Sender: TObject);
    procedure btnSalvarClick(Sender: TObject);
    procedure tvMenusClick(Sender: TObject);
    procedure CbTodosClick(Sender: TObject);
    procedure btnDesfazerClick(Sender: TObject);
    procedure tvMenusChange(Sender: TObject; Node: TTreeNode);
    procedure FormShow(Sender: TObject);
  private
    semDireitosTela : Boolean;
    procedure CarregarMenus;
    procedure LimparLista;
    procedure HabilitarOpcoes(const tof : Boolean);
    function retornaNoPai(codPai: Integer): TTreeNode;
    procedure CarregarDireitosGrupoMenu(const codGrupo, codMenu: Integer);
    procedure verificarPermissaoMenuPai(const no: TTreeNode);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmPermissoesGrupos: TFrmPermissoesGrupos;

implementation

uses SqlExpr, DmGeral, Math, StrUtils;

{$R *.dfm}

procedure TFrmPermissoesGrupos.btnFecharClick(Sender: TObject);
begin
  Close;
end;

function TFrmPermissoesGrupos.retornaNoPai(codPai : Integer) : TTreeNode;
var
  i : Integer;
begin
  Result := nil;
  for i := 0 to Pred(tvMenus.Items.Count) do
    if Integer(tvMenus.Items[i].Data) = codPai then
    begin
      Result := tvMenus.Items[i];
      Exit;
    end;
end;

procedure TFrmPermissoesGrupos.CarregarMenus;
var
  no : TTreeNode;
begin
  tvMenus.Items.Clear;
  with DtmGeral.SQLQ_Auxiliar do begin
    Close;
    SQL.Text := 'SELECT MEN_CODIGO, MEN_CAPTION, MEN_CODIGOPAI FROM MENUS ORDER BY MEN_CODIGO';
    Open;
    First;
    while not Eof do begin
      if FieldByName('MEN_CODIGOPAI').AsString = '' then
        tvMenus.Items.AddObject(nil, FieldByName('MEN_CAPTION').AsString, Pointer(FieldByName('MEN_CODIGO').AsInteger))
      else begin
        no := retornaNoPai(FieldByName('MEN_CODIGOPAI').AsInteger);
        if Assigned(no) then
          tvMenus.Items.AddChildObject(no, FieldByName('MEN_CAPTION').AsString, Pointer(FieldByName('MEN_CODIGO').AsInteger));
      end;
      Next;
    end;
  end;
end;

procedure TFrmPermissoesGrupos.HabilitarOpcoes(const tof : Boolean);
begin
  cbConsultar.Checked := False;
  cbConsultar.Enabled := tof;
  cbIncluir.Checked := False;
  cbIncluir.Enabled := tof;
  cbAlterar.Checked := False;
  cbAlterar.Enabled := tof;
  cbExcluir.Checked := False;
  cbExcluir.Enabled := tof;
end;

procedure TFrmPermissoesGrupos.FormCreate(Sender: TObject);
begin
  dsLookupGrupos.DataSet.Open;
  btnSalvar.Enabled := False;
  HabilitarOpcoes(False);
  semDireitosTela := False;
end;

procedure TFrmPermissoesGrupos.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  dsLookupGrupos.DataSet.Close;
end;

procedure TFrmPermissoesGrupos.LimparLista;
begin
  tvMenus.Items.Clear;
end;

procedure TFrmPermissoesGrupos.dbGruposKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = #27 then
  begin
    dbGrupos.KeyValue := -1;
    btnSalvar.Enabled := False;
    HabilitarOpcoes(False);
    LimparLista;
  end;
end;

procedure TFrmPermissoesGrupos.dbGruposCloseUp(Sender: TObject);
begin
  if dbGrupos.Text <> '' then
    CarregarMenus;
end;

procedure TFrmPermissoesGrupos.CarregarDireitosGrupoMenu(const codGrupo, codMenu : Integer);
begin
  with DtmGeral.SQLQ_Auxiliar do begin
    Close;
    SQL.Text := 'SELECT PG_NOVO, PG_ALTERAR, PG_APAGAR, PG_CONSULTAR ' +
                   '  FROM PERMISSOES_GRUPOS ' +
                   ' WHERE GRU_CODIGO = ' + IntToStr(codGrupo) +
                   '   AND MEN_CODIGO = ' + IntToStr(codMenu);
    Open;
    if not IsEmpty then begin
      semDireitosTela := False;
      cbConsultar.Checked := FieldByName('PG_CONSULTAR').AsString='S';
      cbIncluir.Checked   := FieldByName('PG_NOVO').AsString='S';
      cbAlterar.Checked   := FieldByName('PG_ALTERAR').AsString='S';
      cbExcluir.Checked   := FieldByName('PG_APAGAR').AsString='S';
    end
    else begin
      semDireitosTela := True;
      cbConsultar.Checked := False;
      cbIncluir.Checked := False;
      cbAlterar.Checked := False;
      cbExcluir.Checked := False;
    end;
  end;
end;

procedure TFrmPermissoesGrupos.verificarPermissaoMenuPai(const no : TTreeNode);
begin
  with DtmGeral.SQLQ_Geral do
  begin
    Close;
    SQL.Text := 'SELECT PG_CONSULTAR FROM PERMISSOES_GRUPOS WHERE GRU_CODIGO = ' +
                   IntToStr(dbGrupos.KeyValue) + ' AND MEN_CODIGO = ' + IntToStr(Integer(no.Data));
    Open;
    if IsEmpty then begin
      Close;
      SQL.Text := 'INSERT INTO PERMISSOES_GRUPOS (GRU_CODIGO, MEN_CODIGO, PG_CONSULTAR) ' +
                     'VALUES (' + IntToStr(dbGrupos.KeyValue) + ', ' + IntToStr(Integer(no.Data)) + ', ' +
                     QuotedStr(IfThen(cbConsultar.Checked,'S','N')) + ')';
      ExecSQL(True);
    end
    else
    if Fields[0].AsString='N' then begin //not Fields[0].AsBoolean then
      Close;
      SQL.Text := 'UPDATE PERMISSOES_GRUPOS SET PG_CONSULTAR = ''S'' ' +
                     ' WHERE GRU_CODIGO = ' + IntToStr(dbGrupos.KeyValue) +
                     '   AND MEN_CODIGO = ' + IntToStr(Integer(no.Data));
      ExecSQL(True);
    end;
  end;
  if Assigned(no.Parent) then
    verificarPermissaoMenuPai(no.Parent);
end;

procedure TFrmPermissoesGrupos.btnSalvarClick(Sender: TObject);
begin
  if not (cbConsultar.Checked) and ((cbIncluir.Checked) or (cbAlterar.Checked) or (cbExcluir.Checked)) then
    cbConsultar.Checked := True;
  with DtmGeral.SQLQ_Auxiliar do
  begin
    Close;
    if semDireitosTela then begin
      Sql.Text := 'INSERT INTO PERMISSOES_GRUPOS (GRU_CODIGO, MEN_CODIGO, PG_NOVO, PG_ALTERAR, PG_APAGAR, PG_CONSULTAR) ' +
                     'VALUES (' + IntToStr(dbGrupos.KeyValue) + ', ' + IntToStr(Integer(tvMenus.Selected.Data)) + ', ' +
                     QuotedStr(IfThen(cbIncluir.Checked,'S','N')) + ', ' + QuotedStr(IfThen(cbAlterar.Checked,'S','N')) + ', ' +
                     QuotedStr(IfThen(cbExcluir.Checked,'S','N')) + ', ' + QuotedStr(IfThen(cbConsultar.Checked,'S','N')) + ')';
      semDireitosTela := false;
    end
    else
      Sql.Text := 'UPDATE PERMISSOES_GRUPOS SET ' +
                     'PG_NOVO = ' + QuotedStr(IfThen(cbIncluir.Checked,'S','N')) + ', ' +
                     'PG_ALTERAR = ' + QuotedStr(IfThen(cbAlterar.Checked,'S','N')) + ', ' +
                     'PG_APAGAR = ' + QuotedStr(IfThen(cbExcluir.Checked,'S','N')) + ', ' +
                     'PG_CONSULTAR = ' + QuotedStr(IfThen(cbConsultar.Checked,'S','N')) +
                     ' WHERE GRU_CODIGO = ' + IntToStr(dbGrupos.KeyValue) +
                     '   AND MEN_CODIGO = ' + IntToStr(Integer(tvMenus.Selected.Data));
    ExecSQL(True);
    if (Assigned(tvMenus.Selected.Parent)) and (cbConsultar.Checked) then
      verificarPermissaoMenuPai(tvMenus.Selected.Parent);
  end;
end;

procedure TFrmPermissoesGrupos.tvMenusClick(Sender: TObject);
begin
  if Assigned(tvMenus.Selected) then begin
    HabilitarOpcoes(True);
    CarregarDireitosGrupoMenu(dbGrupos.KeyValue, Integer(tvMenus.Selected.Data));
    btnSalvar.Enabled := True;
  end;
end;

procedure TFrmPermissoesGrupos.CbTodosClick(Sender: TObject);
begin
  cbIncluir.Checked   := CbTodos.Checked;
  cbAlterar.Checked   := CbTodos.Checked;
  cbConsultar.Checked := CbTodos.Checked;
  cbExcluir.Checked   := CbTodos.Checked;
end;

procedure TFrmPermissoesGrupos.btnDesfazerClick(Sender: TObject);
begin
  tvMenusClick(tvMenus);
end;

procedure TFrmPermissoesGrupos.tvMenusChange(Sender: TObject;
  Node: TTreeNode);
begin
  tvMenusClick(tvMenus);
end;

procedure TFrmPermissoesGrupos.FormShow(Sender: TObject);
begin
  Self.Left := 238;
  Self.Top  := 91;
end;

end.
